package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang3.StringUtils;



public class FileAction {

	public static void main(String[] args) throws FileNotFoundException {
		// file.mkdir();

		// 定义文件流
		// FileOutputStream file = null;
		// 定义输入
		
		// OutputStream out = null;
		// 定义HTTPurl的链接

		try(OutputStream file= new FileOutputStream("E:/software/archivesDownload/ces.jpg");) {
			URL url = new URL(
					"http://imgs-1251097942.cos.ap-guangzhou.myqcloud.com/test/saasag/2021/8/30/preview_baaec7b4-5c8d-4036-840a-8510aee38f99.jpg");
			// 获取url
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			// url的链接
			urlConnection.connect();
			try(InputStream is = urlConnection.getInputStream();){
				byte[] buffers = new byte[1024];
				int len;
				// 读取数据，并下载
				while ((len = is.read(buffers)) != -1) {
					file.write(buffers, 0, len);
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		zipFiles("E:/software/archivesDownload/微摇村1", "E:/software/archivesDownload/微摇村1/微摇村1.zip");

	}

	private static void zipFile(ZipOutputStream zipOutputStream, File file, String parentFileName) {
		try (FileInputStream in = new FileInputStream(file);) {
			ZipEntry zipEntry = new ZipEntry(parentFileName);
			zipOutputStream.putNextEntry(zipEntry);
			int len;
			byte[] buf = new byte[8 * 1024];
			while ((len = in.read(buf)) != -1) {
				zipOutputStream.write(buf, 0, len);
			}
			zipOutputStream.closeEntry();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 递归压缩目录结构
	 * 
	 * @param zipOutputStream
	 * @param file
	 * @param parentFileName
	 */
	private static void directory(ZipOutputStream zipOutputStream, File file, String parentFileName) {
		File[] files = file.listFiles();
		String parentFileNameTemp = null;
		for (File fileTemp : files) {
			if (fileTemp.getName().indexOf(".zip") != -1) {
				continue;
			}
			parentFileNameTemp = StringUtils.isEmpty(parentFileName) ? fileTemp.getName()
					: parentFileName + "/" + fileTemp.getName();
			if (fileTemp.isDirectory()) {
				directory(zipOutputStream, fileTemp, parentFileNameTemp);
			} else {
				zipFile(zipOutputStream, fileTemp, parentFileNameTemp);
			}
		}
	}

	/**
	 * 压缩文件目录
	 * 
	 * @param source 源文件目录（单个文件和多层目录）
	 * @param destit 目标目录
	 */
	public static void zipFiles(String source, String destit) {
		File file = new File(source);
//		ZipOutputStream zipOutputStream = null;
//		FileOutputStream fileOutputStream = null;
		try (FileOutputStream fileOutputStream = new FileOutputStream(destit);
				ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);) {
//			fileOutputStream = new FileOutputStream(destit);
//			zipOutputStream = new ZipOutputStream(fileOutputStream);
			if (file.isDirectory()) {
				directory(zipOutputStream, file, "");
			} else {
				zipFile(zipOutputStream, file, "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 递归删除目录下的所有文件及子目录下所有文件
	 * 
	 * @param dir 将要删除的文件目录
	 * @return boolean Returns "true" if all deletions were successful. If a
	 *         deletion fails, the method stops attempting to delete and returns
	 *         "false".
	 */
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}
}
